import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';

import 'player_widget.dart';

typedef void OnError(Exception exception);

const kUrl4 =
    'IyFBTVIKPDI8JlTk8ePkZ0/88CIzMgAAAAAnrANUAAAADOHbIxA8Kq2DhHf4B4LQ61WAeaZVroHp0SKoHnUiuD+Io8a4gDwoNjQX51KSgeHAa+tOdXV6JDAPQeKMpO9Q9eT8+nsgPCYmZCZ/EUth4MNK22er+w9bvlDsI42sLdBadiyZCCA8IC1tHnsdM8H3k/zwRUKTK7pWf/2zMsukSOnKL9B3EDwgMIHmfk89ofMk9O+jZcQIe7aOH/4AoDS+Az5Z7KfgPB4SUnTeiCsh4DZ7L1MbHWJRzAJrWm9eKhl2wpcKfRA8KLZ0/fQSHwH6MFoqVxAcQv0TKi53LTP0oZsqQv2YIDwnehkzvv5NofWo+mRv/Az1Df/xgLTFRY82tYqBImuwPNg0Kkb0nBrB+yZqsj5iFP28V36OS1Np4mMPR0/J0iA8IFORHrqqDwH0VTkVhnopx3ODjHhxQcLUleWjvrEx0DxEIhpDtOqA4ftyyuAecj3Y+gyzEydAjxEtZWArsuQAPC5KNSB4MgDh+NIk8bT9QTGxqVwPtq9PgoeHPHijEdA8IBJONXakU6Hod+QLiXR9JXoJ4WmErvyOykfVT9XAQDwgUC4ceFI4Qdc/uyX1XgVBjde7Ioz/BwX/Ra+bF9LwPCF5gi4SqUDh6NfIvT5czDXwC9olaQUi/BCO4kePE9A8FCyNJDGoMkH1+SoFtzTlJarakhUZuNXkvlJUmYAxIDz4MHkq8GcVqHLd6/r4y1xTTt1pHJKF803guNdvho4QPCYSLyLrugBh4Po6kVDuWg+nQfO8HMOOhjhmkO1P8cA82DZvfLqKxOHzLbwXkImuPcuMEwaB11cSIECboIITMDwuI51Keh0TwPmFK88arZ10BM3VscIvUFzdeW73yw4QPBo01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jm+aczMYzYtNyyyg5hVxgCZoA8eGwk+M5+gEl3gF/dCvqI1x9Bx5Oe+UtbqGBYZRlgoDw0aF3QRZgFEs0L/0bqL6KwUBHQjm4ewNa3jtvMp0lQPC4piO5+ugWh5y/f3UKzy83SkMI0HvtZx4pjAVzTviA8AjgasbyMuvhn/A/X6h3cl5dx331t1GjHlkSsU39gEDzgUhXuAGc38twqHzK4/ZKENKDoWRYhFAGKgWDjn4uQPOBVkbtVd/vaV7B64y4OUdslTM3xXCspus/gaPso74A84FeWlgD7JI0ydi/cyOTjt73e6qIwyAuBcOvQ2zeOgDzgXBymAG8N+sWDK9rLIX9DxtLYIis8l+krtZL7VkGwPN5WEZ4Bg482iSqPd6KeR7t8i4xqwoHkWkIjD6ED0gA84FwTRgHhaM0zhlrTi6uG4hOBeW9uNb5/z5vAyeBjADzeVhPGATvwlomBKkp4yWT8+Vrh+AlYQfB3nSE6DJzQPAhdwZYBNj3LVNSViFQ5NkcQi4czAD00NZRhV5J7R6A8CEoXlgGCebLMf/n2h9cI6MY3mUxY6UNhoyVb09Zc0DwEShfuAOaWAf8t2osD887Fo7wzXlreCLf6csQ2+otwPAQ7rEYDUpapZk5fcI2moKHYukjaw83V+9AW1wibFSA8Ah2GTKueEeHfvCxzj1Kp+1zYBP8kBUzxPFjxjtMaoDzgDiaDV5VsIelo2Zriuj6BpLq8ofQGuA64uzTn9rZwPAQSZJYDeltB49JEZKrzqgeylECRQrm48LyykM6bAnA83g40TgdktCDwzqsPP9q7ZL/sjVH++AXNM0cOQKNSkDwePjoeEp+zweTE16xmupocCX4jqGjQxeXbrRBtSEFwPDBhqhQl2Dph7ypL0KJCqGdu+tLEIrNL0UjFMx9kN6A=';

void main() {
  runApp(new MaterialApp(home: new ExampleApp()));
}

class ExampleApp extends StatefulWidget {
  @override
  _ExampleAppState createState() => new _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AMR 播放"),
      ),
      body: Container(
        child: IconButton(
          onPressed: () => _play(),
          iconSize: 64.0,
          icon: new Icon(Icons.play_arrow),
          color: Colors.cyan,
        ),
      ),
    );
  }

  void _play() async {
    final dir = await getApplicationDocumentsDirectory();
    final file = new File('${dir.path}/audio.amr');

    await file.writeAsBytes(base64Decode(kUrl4));

    print(file.path);
    AudioPlayer audioPlayer = AudioPlayer();
    await audioPlayer.play(
      file.path,
      isLocal: true,
    );
  }
}
